package com.navi.loaddata.job;


import com.navi.loaddata.core.job.QuartzManage;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadService.IEtlConfServices;
import com.navi.loaddata.loadService.impl.shtWipLoad.LoadShtWipOpeService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@DisallowConcurrentExecution//禁止并发
public class LoadShtWipOpeData  implements Job {

    private static final Logger logger = LoggerFactory.getLogger(LoadShtWipOpeData.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private QuartzManage quartzManage;

    @Autowired
    private IEtlConfServices etlConfServices;

    @Autowired
    private LoadShtWipOpeService loadShtWipOpeService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        EtlConfD etlConfD = (EtlConfD) jobDataMap.get("etlConf");
        etlConfD = etlConfServices.getNewConf(etlConfD);
        if(null==etlConfD) {
            logger.error("找不到该JOB。");
            return;
        }

        if ("N".equals(etlConfD.getRunFlg())) {
            etlConfD = etlConfServices.beginJob(etlConfD);
            if (StringUtils.isEmpty(etlConfD.getQueryMinute())) {
                return;
            }
            startETL(etlConfD);
        } else {
            logger.info("Job {}-{} is running.", etlConfD.getJobGroupName(), quartzManage.getRealJobName(etlConfD));
        }
    }

    private void startETL(EtlConfD etlConfD) {
        int countSize = 0;

        String errorMsg=null;
        try {
            errorMsg="loadShtWipOpeToTmp表message:";
            countSize+= loadShtWipOpeService.loadMain1(etlConfD);
            //todo  做判断  0-11,11的时候执行，并置为0

            if (StringUtils.isEmpty(etlConfD.getExt1())) {
                etlConfD.setExt1(String.valueOf(0));
            }
            if (Integer.valueOf(etlConfD.getExt1()) >= 11) {
                errorMsg = "loadShtWipOpeToSum表message:";
                loadShtWipOpeService.loadMain2(etlConfD);
                etlConfD.setExt1(String.valueOf(0));
            }else {
                etlConfD.setExt1(String.valueOf(Integer.valueOf(etlConfD.getExt1()) + 1));
            }
            etlConfD.setCountSize(countSize);
            etlConfServices.endJob(etlConfD);
            etlConfServices.addHis(etlConfD, null);
        } catch (Exception e) {
            errorMsg+=e.getMessage();
            logger.error(loggerUtil.stackTraceToString(e));
            logger.error("Job {} [{}]-[{}] FAIL TO SYNCHRONIZE.", etlConfD.getProjectName() + etlConfD.getJobGroupName() + etlConfD.getJobName(), etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
            etlConfServices.addHis(etlConfD, errorMsg);
            etlConfD = etlConfServices.getNewConf(etlConfD);
            etlConfD.setCountSize(0);
            etlConfServices.endJob(etlConfD);
        }


    }

}
